Page({

  /**
   * 页面的初始数据
   */
  data: {
    serchKey: '',
    reSou: [],
    searchKeyList: [ 
      'ئاتامان',
      'ئون كۈنلۈك ھالاكەت',
      'دۇنيادىكى ئەڭ سىرلىق 24 دەرىس', 
      'خەيىرلىك كەچ', 
      'بەخىت نەدە؟', 
      'بىشەكشىرىن', 
      'ياغاچ ئات'
    ],
    pageNo: 1,
    pageSize: 15,
    status: 1,
  },

  // 获取热搜内容
  getPost() {
    let that = this;
    wx.request({
      url: 'https://api-kitap.qiyat.cn/kitap',
      data: {
        pageNo: this.data.pageNo,
        pageSize: this.data.pageSize,
        status: this.data.status,
        categoryId: '',
        recommend: '',
        is_free: -1,
        video_source: '',
        isLanguageTypeValue: -1,
        keyWord: ''
      },
      header: {
        'content-type': 'application/json'
      },
      success (res) {
        if(res.statusCode == 200) {
          that.setData({ reSou: res.data.data })
        }
      }
    })
  },

  // 点击书籍
  onBook(item) {
    let postId = item.currentTarget.dataset.item.id;
    wx.navigateTo({
      url: `/pages/audioList/audioList?postId=`+postId,
    })
  },

  // 搜索内容
  onInput(event) {
    let searchKeyWord = event.detail.value;
    this.setData({ serchKey: searchKeyWord });

    // 监听删除
    if (searchKeyWord.length == 0) {
      this.getPost();
    }

    let that = this;
    if(searchKeyWord !== '') {
      let keyWord = searchKeyWord
      wx.request({
        url: 'https://api-kitap.qiyat.cn/getKitapByKey',
        method: 'GET',
        data: { keyWord },
        success(res) {
          if(res.statusCode == 200) {
            that.setData({ reSou: res.data })
          }
        }
      })
    }
    
  },

  // 点击搜索记录
  searchKey(item) {
    let searchKeyWord = item.currentTarget.dataset.item;
    this.setData({ serchKey: searchKeyWord });

    let keyWord = searchKeyWord;
    let that = this;
    wx.request({
      url: 'https://api-kitap.qiyat.cn/getKitapByKey',
      method: 'GET',
      data: { keyWord },
      success(res) {
        if(res.statusCode == 200) {
          that.setData({ reSou: res.data });
        }
      }
    })

  },

  // 点击搜索
  onSearch() {
    let keyWord = this.data.serchKey;
    let that = this;
    if(keyWord =='') return;
    wx.request({
      url: 'https://api-kitap.qiyat.cn/getKitapByKey',
      method: 'GET',
      data: { keyWord },
      success(res) {
        if(res.statusCode == 200) {
          that.setData({ reSou: res.data });
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getPost();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: 'قىيات ئاۋازلىق ئەسەر',
      path: '/pages/search/search',
      imageUrl: 'https://mmecoa.qpic.cn/sz_mmecoa_png/iauAIibMdU78thJHXKBcIfYLNA6zH3pbFllMLlSibhVriaGwtS0bM1tLLyz6OSlQ74icF6c4Yic4icLokYGvb2jXnY8bA/640?wx_fmt=png&amp;from=appmsg'
    };
  }
})